package com.axxc.jpa.config;

import java.util.HashMap;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.axxc.jpa.out.common.constant.RabbitMQConstant;

/**
 * 
 * @ClassName RabbitMqConfig
 * @Description //RabbitMq 配置导入的类为amqp.core包里的类
 * @author xxc
 * @data 2018年8月3日下午11:53:23
 * @see https://www.cnblogs.com/boshen-hzb/p/6841982.html
 */
@Configuration
public class RabbitMqConfig {

	final static String queueName = "hello";

	@Bean
	public Queue helloQueue() {
		return new Queue("hello");// 声明一个hello的队列
	}

	@Bean
	public Queue enumuserQueue() {
		return new Queue("enumuser");
	}

	@Bean
	public Queue testpersonQueue() {
		return new Queue("test_person");
	}

	@Bean
	public Queue userQueue() {
		return new Queue("user");
	}

	@Bean
	public Queue stringMsgQueue() {
		return new Queue("string_msg_queue");
	}

	// ===============以下是验证topic Exchange的队列==========
	@Bean
	public Queue queueMessage() {
		return new Queue("topic.message");
	}

	@Bean
	public Queue queueMessages() {
		return new Queue("topic.messages");
	}
	// ===============以上是验证topic Exchange的队列==========

	// ===============以下是验证Fanout Exchange的队列=========
	@Bean
	public Queue AMessage() {
		return new Queue("fanout.A");
	}

	@Bean
	public Queue BMessage() {
		return new Queue("fanout.B");
	}

	@Bean
	public Queue CMessage() {
		return new Queue("fanout.C");
	}
	// ===============以上是验证Fanout Exchange的队列=========

	@Bean
	TopicExchange exchange() {
		return new TopicExchange("exchange");// 交换机
	}

	@Bean
	FanoutExchange fanoutExchange() {
		return new FanoutExchange("fanoutExchange");// 扇出模式交换机
	}

	/**
	 * 将队列topic.message与exchange绑定，binding_key为topic.message,就是完全匹配
	 * 
	 * @param queueMessage
	 * @param exchange
	 * @return
	 */
	@Bean
	Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
		return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
	}

	/**
	 * 将队列topic.messages与exchange绑定，binding_key为topic.#,模糊匹配
	 * 
	 * @param queueMessage
	 * @param exchange
	 * @return
	 */
	@Bean
	Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {
		return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
	}

	@Bean
	Binding bindingExchangeA(Queue AMessage, FanoutExchange fanoutExchange) {
		return BindingBuilder.bind(AMessage).to(fanoutExchange);
	}

	@Bean
	Binding bindingExchangeB(Queue BMessage, FanoutExchange fanoutExchange) {
		return BindingBuilder.bind(BMessage).to(fanoutExchange);
	}

	@Bean
	Binding bindingExchangeC(Queue CMessage, FanoutExchange fanoutExchange) {
		return BindingBuilder.bind(CMessage).to(fanoutExchange);
	}

	// |-利用死信队列转化到其他队列,实现订单的延迟处理-|
	@Bean
	public Queue repeatTradeQueue() {
		return new Queue(RabbitMQConstant.DEFAULT_REPEAT_TRADE_QUEUE_NAME, true, false, false);// 声明一个转发队列
	}

	@Bean
	public DirectExchange defaultExchange() {
		return new DirectExchange(RabbitMQConstant.DEFAULT_EXCHANGE, true, false);
	}

	@Bean
	public Binding dereadTradeBinding() {
		return BindingBuilder.bind(repeatTradeQueue()).to(defaultExchange())
				.with(RabbitMQConstant.DEFAULT_REPEAT_TRADE_QUEUE_NAME);
	}

	@Bean
	public Queue deadLetterQueue() {
		HashMap<String, Object> hashMap = new HashMap<String, Object>() {
			private static final long serialVersionUID = 1L;
			{
				put("x-dead-letter-exchange", RabbitMQConstant.DEFAULT_EXCHANGE);
				put("x-dead-letter-routing-key", RabbitMQConstant.DEFAULT_REPEAT_TRADE_QUEUE_NAME);
			}
		};
		return new Queue(RabbitMQConstant.DEFAULT_DEAD_LETTER_QUEUE_NAME, true, false, false, hashMap);
	}

	@Bean
	public Binding deadLetterBinding() {
		return BindingBuilder.bind(deadLetterQueue()).to(defaultExchange())
				.with(RabbitMQConstant.DEFAULT_DEAD_LETTER_QUEUE_NAME);
	}
}
